package code.main.controller.table
{
	import code.app.AppCtrlList;
	import code.app.AppDmList;
	import code.app.AppView;
	import code.common.effects.Effects;
	import code.main.controller.alert.AlertCtrl;
	import code.main.modle.dnAlg.DnAlg;
	import code.main.modle.lang.Language;
	import code.main.modle.table.CardVo;
	import code.main.modle.table.Player;
	import code.main.modle.table.TableData;
	import code.main.view.table.TableView;
	import code.module.motion.FaceEmotionManager;
	import code.module.sound.SoundManager;
	
	import com.greensock.easing.Circ;
	import com.yy.manager.UiManager;
	import com.yy.yyui.button.SwitchButton;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.media.Sound;
	
	/**
	 *座位控制器 
	 * @author YY
	 * 
	 */
	public class SeatCtrl
	{
		/**操作类型**/
		public static var OPTION_TYPE_CLICK_SEAT_BTN:int = 0;//点击坐下按钮
		public static var OPTION_TYPE_CLICK_GRAB_BANKER:int = 1;//点击抢庄按钮
		public static var OPTION_TYPE_CLICK_GIVE_UP_BANK:int = 2;//点击放弃抢庄按钮
		public static var OPTION_TYPE_CLICK_BET:int = 3;//点击下注按钮
		public static var OPTION_TYPE_CLICK_CARD:int = 4;//点击自己的牌
		public static var OPTION_TYPE_CLICK_SHOW_CARD:int = 5;//点击亮牌
		public static var OPTION_TYPE_SETTLEMENT_COMPLETE:int = 6;//结算完毕
		public static var OPTION_TYPE_CLICK_UP_SEAT:int = 7;//点击站起
		public static var OPTION_TYPE_CLICK_GO_HALL:int = 8;//点击进入大厅
		
		/**显示操作面板**/
		public static var SHOW_OPTION_PANEL_GRAB_BANKER:int = 0;//抢庄
		public static var SHOW_OPTION_PANEL_BET:int = 1;//下注
		public static var SHOW_OPTION_PANEL_SHOW_CARD:int = 2;//亮牌
		public static var SHOW_OPTION_PANLE_HIDE:int = 3;//隐藏所有操作面板
		
		
		public static var SEAT_NUMB:int = 7;//隐藏所有操作面板
		
		private var _parent:DisplayObjectContainer;
		private var _tableView:TableView;
		private var _cb:Function;//回调函数
		
		private var _chipsCtrl:ChipsCtrl;//筹码控制器
		private var _buyInDlgCtrl:BuyInDlgCtrl;//买入控制器
		private var _addBetCtrl:AddBetCtrl;//下注控制器
		private var _timeCtrl:TimeCtrl;//时间控制器
		private var _emotionCtrl:FaceEmotionManager;//表情控制器
		
		private var _tableData:TableData;//游戏数据
		private var _player:Player;//玩家数据
		private var _tempSeatId:int;//点击坐下按钮 临时 座位id
		public function SeatCtrl(parent:DisplayObjectContainer, cb:Function)
		{
			_parent = parent;
			_cb = cb;
			init();
			addEvent();
		}
		
		private function init():void {
			_player = AppDmList.getInstance().player;
			_tableData = AppDmList.getInstance().tableData;
			_tableView = new TableView();
			_chipsCtrl = new ChipsCtrl(_tableView);
			_buyInDlgCtrl = new BuyInDlgCtrl();
			_addBetCtrl = new AddBetCtrl(addBetCB, _tableView.addBetUi);
			_timeCtrl = new TimeCtrl(_tableView.timeUi);
			_emotionCtrl = new FaceEmotionManager(_tableView);
		}
		private function addEvent():void {
			var i:int;
			var len:int = 7;
			for(i=0; i<len; i++) {
				_tableView.tableView["seatBtn" + i].addEventListener(MouseEvent.CLICK, clickSeatFunc);
			}
			_tableView.upSeatBtn.addEventListener(MouseEvent.CLICK, clickUpFunc);
			_tableView.goHallBtn.addEventListener(MouseEvent.CLICK, clickGoHallFunc);
			_tableView.soundSwitchBtn.addEventListener(MouseEvent.CLICK, clickSoundSwitch);
		}
		
		/**
		 *点击声音开关 
		 * @param e
		 * 
		 */
		private function clickSoundSwitch(e:Event):void {
			SoundManager.SOUND_SWITCH = _tableView.soundSwitchBtn.select;
		}
		
		/**
		 *点击站起 
		 * @param e
		 * 
		 */
		private function clickUpFunc(e:Event):void {
			trace("点击站起");
			if(_player.isGaming)
			{				
				AppCtrlList.getInstance().alertCtrl.sysMsgBox(Language.ROOM_STAND_TIPS[0]);
			}
			else
			{
				_cb(OPTION_TYPE_CLICK_UP_SEAT);
			}
		}
		/**
		 *点击返回大厅 
		 * @param e
		 * 
		 */
		private function clickGoHallFunc(e:Event):void {
			trace("点击返回大厅");
			if(_player.isGaming)
			{
				AppCtrlList.getInstance().alertCtrl.sysMsgBox(Language.ROOM_STAND_TIPS[0]);
			}
			else
			{
				_cb(OPTION_TYPE_CLICK_GO_HALL);
			}			
		}
		
		/**
		 * 下注回调
		 * @param data
		 * 
		 */
		private function addBetCB(data:int):void {
			_timeCtrl.stopTime();
			_cb(OPTION_TYPE_CLICK_BET, {value:data});
			_addBetCtrl.hideRaiseCall();
		}
		
		/**
		 * 买入回调
		 * @param obj
		 * 
		 */
		private function buyInCallBack(obj:Object=null):void {
			_cb(OPTION_TYPE_CLICK_SEAT_BTN, {money:obj, id:_tempSeatId});
		}
		
		/**
		 *点击坐下按钮 
		 * @param e
		 * 
		 */
		private function clickSeatFunc(e:Event):void {
			var btn:Object = e.currentTarget;
			var id:int;
			switch(btn) {
				case _tableView.tableView["seatBtn0"]:
					id = 1;
					break;
				case _tableView.tableView["seatBtn1"]:
					id = 2;
					break;
				case _tableView.tableView["seatBtn2"]:
					id = 3;
					break;
				case _tableView.tableView["seatBtn3"]:
					id = 4;
					break;
				case _tableView.tableView["seatBtn4"]:
					id = 5;
					break;
				case _tableView.tableView["seatBtn5"]:
					id = 6;
					break;
				case _tableView.tableView["seatBtn6"]:
					id = 7;
					break;
			}
			if(_player.gameMoney >= _player.tableVo.minCarring) {
				_tempSeatId = id;
				if(!_player.isSeat) {
					UiManager.getPopUpManager().addPopUp(_buyInDlgCtrl.getView(buyInCallBack), true);
				}else {
					playerSeatEffect(_player.seatId);
				}
				
			}else {
				AppCtrlList.getInstance().alertCtrl.sysMsgBox(Language.ROOM_BUY_IN_TIPS[6], null, AlertCtrl.BTN_TYPE_OK_CANCLE, Language.ROOM_BUY_IN_TIPS[4],Language.ROOM_BUY_IN_TIPS[5]);
			}
		}
		
		
		/**
		 *显示桌子 
		 * 
		 */
		public function show():void {
			if(_parent) {
				_parent.addChildAt(_tableView, 0);
			}
		}
		
		/**
		 *显示比赛玩家 
		 * @param tableState 游戏状态
		 * @param palyers
		 * @return 
		 * 
		 */
		public function showSeat():void {
			showAllSeat(_tableData.getMatchList());
			switch(_tableData.currentGameState) {
				case TableData.GAME_STATE_START:
					break;
				case TableData.GAME_STATE_GRAB_BANK:
					
					break;
				case TableData.GAME_STATE_BET:
					showTableInShowCardState();
					break;
				case TableData.GAME_STATE_SHOW_CARD:
					showTableInShowCardState();
					break;
				case TableData.GAME_STATE_SETTLEMENT:
					break;
			}
			showTableComplete();
		}
		
		/**
		 *显示桌子信息 在 亮牌阶段 
		 * 
		 */
		private function showTableInShowCardState():void{
			var list:Array = _tableData.getMatchList(false);
			var i:int;
			var len:int = list.length;
			var p:Player;
			for(i=0; i<len; i++) {
				p = list[i];
				//显示玩家下的注
				if(!p.isBanker && p.isBetMoney > 0) {
					_chipsCtrl.showChips(p.seatId, p.isBetMoney);
				}
				if(p.cards.length>0) {
					//显示亮玩牌的人
					showCardByLen(p.seatId, 5);
					showCowUi(true, p.seatId, p.cowNumb);
				}else {
					//显示没有亮牌的人
					showHideCard(p.seatId, 5);
				}
			}
			
		}
		
		/**
		 *桌子信息显示完毕 
		 * 
		 */
		public function showTableComplete():void{
			AppCtrlList.getInstance().tableCtrl.showTable();
		}
		
		/**
		 *显示每个座位的信息 
		 * @param arr
		 * @return 
		 * 
		 */
		private function showAllSeat(arr:Array):void {
			var i:int;
			var len:int = arr.length;
			var player:Player;
			for(i=0; i<len; i++) {
				player = arr[i];
				if(player) {
					showASeat(player);
				}
			}
		}
		/**
		 *显示一个座位的信息 
		 * 
		 */
		public function showASeat(player:Player):void {
			_tableView.showUserInfo(player);
			
			if(player.gameId == _player.gameId) {
				_tableView.setOptionPos(player.seatId);
				_tableView.playerSeatEffect(player.seatId);
			}
		}
		
		/**
		 *隐藏一个位置 
		 * @param seatId
		 * 
		 */
		public function hideASeat(seatId:int):void {
			_tableView.setSeatVisible(seatId, false);
			
			resetSeat(seatId);
			
		}
		/**
		 *隐藏所有位置 
		 * 
		 */
		public function hideAllSeat():void {
			resetNewGame();
			var i:int;
			var len:int = SEAT_NUMB;
			for(i=0; i<len; i++) {
				hideASeat(i+1);
			}
		}
		
		/**
		 *隐藏桌子 
		 * 
		 */
		public function hide():void {
			if(_parent && _parent.contains(_tableView)) {
				_parent.removeChild(_tableView);
			}
		}
		
		/**
		 *设置亮牌按钮显示否 
		 * @param boo
		 * 
		 */
		public function showShowCardBtn(boo:Boolean, clickBoo:Boolean):void {
			_tableView.setShowCardBtnVisible(boo);
			var mc:SimpleButton = _tableView.tableView["showCardBtn"];
			if(clickBoo) {
				mc.addEventListener(MouseEvent.CLICK, clickShowCardBtnFunc);
				mc.filters = null;
				mc.mouseEnabled = true;
			}else {
				mc.removeEventListener(MouseEvent.CLICK, clickShowCardBtnFunc);
				mc.filters = Effects.BUTTON_DISABLE_FILTER;
				mc.mouseEnabled = true;
				mc.tabEnabled = false;
			}
		}
		
		/**
		 *点击亮牌按钮 
		 * @param e
		 * 
		 */
		private function clickShowCardBtnFunc(e:Event):void {
			_timeCtrl.stopTime();
			showShowCardBtn(false, false);
			_cb(OPTION_TYPE_CLICK_SHOW_CARD, null);
		}
		
		
		/**
		 *显示抢庄画面 
		 * 
		 */
		public function showGrabBankerUi(boo:Boolean):void {
			trace("==>显示抢庄控制按钮");
			_tableView.setGrabUiVisible(boo);
			if(boo) {
				_tableView.resetBankerLogo();
				_tableView.grabBankBtn.addEventListener(MouseEvent.CLICK, clickGrabBankFunc);
				_tableView.giveUpBankBtn.addEventListener(MouseEvent.CLICK, clickGrabBankFunc);
			} else {
				removeGrabListener();
			}
		}
		/**
		 *点击抢庄与放弃按钮 
		 * @param e
		 * 
		 */
		private function clickGrabBankFunc(e:Event):void {
			var obj:Object = e.currentTarget;
			var boo:Boolean = false;
			switch(obj) {
				case _tableView.grabBankBtn:
					boo = true;
					break;
				case _tableView.giveUpBankBtn:
					boo = false;
					break;
			}
			showGrabBankerUi(false);
			_timeCtrl.stopTime();
			if(boo) {
				_cb(OPTION_TYPE_CLICK_GRAB_BANKER,{});
			}else {
				_cb(OPTION_TYPE_CLICK_GIVE_UP_BANK,{});
			}
		}
		
		/**
		 *移除抢庄侦听 
		 */
		private function removeGrabListener():void {
			_tableView.grabBankBtn.removeEventListener(MouseEvent.CLICK, clickGrabBankFunc);
			_tableView.giveUpBankBtn.removeEventListener(MouseEvent.CLICK, clickGrabBankFunc);
		}
		
		
		/**
		 *显示隐藏下注按钮 
		 * @param boo
		 * 
		 */
		public function showBetBtn(boo:Boolean):void {
			if(boo) {
				_addBetCtrl.showRaiseCall(1, _player.tableMoney, 10);
			}else {
				_addBetCtrl.hideRaiseCall();
			}
		}
		
		/**
		 *显示操作面板 
		 * @param type
		 * 
		 */
		public function showOptionPanle(type:int):void {
			hideAllOptionPanle();
			switch(type) {
				case SHOW_OPTION_PANEL_GRAB_BANKER:
					showGrabBankerUi(true);
					break;
				case SHOW_OPTION_PANEL_BET:
					showBetBtn(true);
					break;
				case SHOW_OPTION_PANEL_SHOW_CARD:
					showShowCardBtn(true, true)
					break;
				default:
			}
		}
		/**
		 *隐藏全部操作面板 
		 * 
		 */
		public function hideAllOptionPanle():void {
			showGrabBankerUi(false);
			showBetBtn(false);
			showShowCardBtn(false,false);
		}
		
		/**
		 *显示倒计时 
		 * 
		 */
		public function showClock(t:int):void {
			trace("玩家:"+_player.seatId+"在玩否:"+_player.isGaming+"  ==>显示倒计时");
			if(_player.isGaming)
			_timeCtrl.startTime(_player.seatId, t, timeOutCB);
		}
		
		/**
		 *隐藏倒计时 
		 * 
		 */
		public function hideClock():void {
			trace("==>隐藏倒计时");
			_timeCtrl.stopTime();
		}
		/**
		 *显示公共倒计时 
		 * @param t
		 * 
		 */
		public function showPublic(t:int):void {
			_tableView.clock.setTime(t);
		}
		
		/**
		 *隐藏公共倒计时 
		 * @param t
		 * 
		 */
		public function hicePublicClock():void {
			_tableView.clock.clockVisible=false;
		}
		
		/**
		 *超时处理 
		 * 
		 */
		private function timeOutCB():void {
			var type:int = _tableData.currentGameState;
			switch(type) {
				case TableData.GAME_STATE_GRAB_BANK:
					_cb(OPTION_TYPE_CLICK_GIVE_UP_BANK,{});
					break;
				case TableData.GAME_STATE_BET:
					addBetCB(_player.tableVo.minBet);
					break;
				case TableData.GAME_STATE_SHOW_CARD:
					clickShowCardBtnFunc(null);
					break;
			}
		}
		
		/**
		 *显示抢庄动画 
		 * @param seatId
		 * @param boo
		 * @return 
		 * 
		 */
		public function showGrabBankerMotion(seatId:int, boo:Boolean):void{
			trace("===》显示抢庄动画");
		}
		
		/**
		 *设置庄家 
		 * @param seatId
		 * 
		 */
		public function setBanker(seatId:int):void {
			_tableView.setBankerLogo(seatId);
		}
		
		/**
		 *设置下注界面 
		 * @param boo
		 * 
		 */
		public function setCanBet(boo:Boolean):void {
			trace("==>显示下注面板");
			showBetBtn(boo);
		}
		
		/**
		 *播放下注动画 
		 * @param seatId
		 * @param bet
		 * 
		 */
		public function showBetMotion(seatId:int, bet:int):void {
			trace("===============》播放下注动画中。。。。");
			_chipsCtrl.playerBet(seatId, bet);
		}
		
		/**
		 *刷新玩家的钱 
		 * @param seatId
		 * @param money
		 * 
		 */
		public function refreshUserTableMoney(seatId:int, money:Number):void {
			_tableView.setMoney(seatId, money);
		}
		
		/**
		 *播放发牌动画 
		 * 
		 */
		public function dealCardMotion():void {
			trace("=================》播放发牌动画中。。。");
		}
		
		/**
		 *播放坐下动画 
		 * @param seatId
		 * 
		 */
		public function playerSeatEffect(seatId:int):void {
			_tableView.playerSeatEffect(seatId);
		}
		
		/**
		 *显示牌 
		 * 
		 * @param arr
		 * @param seatId
		 * 
		 */
		public function showCards(arr:Array, seatId:int, showBoo:Boolean=true):void {
			var cardMc:MovieClip = _tableView.getShowCardMc(seatId);
			cardMc.visible = showBoo;
			var i:int;
			var len:int = arr.length;
			var vo:CardVo;
			var mc:MovieClip;
			for(i=0; i<len; i++) {
				vo = arr[i];
				mc = cardMc["card"+i];
				mc.visible = true;
				mc.cardVo = vo;
				if(vo.id > 10) {
					mc.cardType.gotoAndStop(4 + vo.color);
				}else {
					mc.cardType.gotoAndStop(vo.color);
				}
				if(vo.color == 1 || vo.color == 3) {
					mc.cardValue.gotoAndStop(13+vo.id);
				}else {
					mc.cardValue.gotoAndStop(vo.id);
				}
				if(vo.selectBoo) {
					mc.y = -10;
				}else {
					mc.y = 0;
				}
				mc.addEventListener(MouseEvent.CLICK, clickCardFunc);
			}
		}
		
		/**
		 *显示指定长度的牌 
		 * @param seatId
		 * @param len
		 * 
		 */
		public function showCardByLen(seatId:int, len:int):void {
			var cardMc:MovieClip = _tableView.getShowCardMc(seatId);
			cardMc.visible = true;
			var len:int;
			var i:int;
			for(i=0; i<len; i++) {
				cardMc["card" + i].visible = true;
			}
			for(i; i<5; i++) {
				cardMc["card" + i].visible = false;
			}
		}
		
		/**
		 *显示隐藏的牌 
		 * 
		 */
		public function showHideCard(seatId:int, count:int):void {
			var i:int;
			var cardMc:MovieClip;
			cardMc = _tableView.getHideCardMc(seatId);
			cardMc.visible = true;
			for(i=0; i<count; i++) {
				cardMc["card" + i].visible = true;
			}
			for(i; i<5; i++) {
				cardMc["card" + i].visible = false;
			}
		}
		
		/**
		 *点击牌 
		 * @param e
		 * 
		 */
		private function clickCardFunc(e:Event):void {
			var mc:MovieClip = MovieClip(e.currentTarget);
			var vo:CardVo = mc.cardVo;
			vo.selectBoo = !vo.selectBoo; 
			_cb(OPTION_TYPE_CLICK_CARD, null);
		}
		
		/**
		 *显隐牛值ui 
		 * @param showBoo
		 * @param seatId
		 * @param cowValue
		 * 
		 */
		public function showCowUi(showBoo:Boolean,seatId:int, cowValue:int=10):void {
			seatId--;
			var mc:MovieClip = _tableView.tableView["cowNumb" + seatId];
			mc.visible = showBoo;
			if(showBoo) {
				mc.gotoAndStop("cow"+cowValue);
			}
		}
		
		/**
		 *开始结算 
		 * 
		 */
		public function startSettlement():void {
			trace("===================>开始结算");
			var banker:Player = _tableData.getBanker();
			var pList:Array = _tableData.getMatchPlayerExceptBanker();
			var tempP:Player;
			startFunc();
			function startFunc():void {
				trace(_player.gameId + ":" + pList);
				tempP = pList.pop();
				if(tempP) {
					var bet:int = 0;
					var odds:int = 1;//赔率
					if(banker.comparisonPlyaer(tempP)) {
						//庄家胜
						odds = DnAlg.getInstance().getOddsByCow(banker.getCowNumb());
						if(odds > 1) {
							//如果赔率大于1 
							//闲家将筹码移动到奖池
							bet = tempP.isBetMoney*odds;
							tempP.tableMoney -= tempP.isBetMoney*(odds-1);
							refreshUserTableMoney(tempP.seatId, tempP.tableMoney);
							_chipsCtrl.playerBet(tempP.seatId,bet, xxFunc); 
							SoundManager.playerSound(SoundManager.MoveToSeat);
						}else {
							bet = tempP.isBetMoney;
							_chipsCtrl.potToPlayer(tempP.seatId, banker.seatId, xxFunc2);
							SoundManager.playerSound(SoundManager.MoveToSeat);
						}
						function xxFunc():void {
							SoundManager.playerSound(SoundManager.MoveToSeat);
							//闲家奖池筹码移动到庄家
							_chipsCtrl.potToPlayer(tempP.seatId, banker.seatId, xxFunc2);
						}
						function xxFunc2():void {
							banker.tableMoney += bet;
							refreshUserTableMoney(banker.seatId, banker.tableMoney);
							startFunc();
						}
					}else {
						//闲家胜
						odds = DnAlg.getInstance().getOddsByCow(tempP.cowNumb);
						bet = tempP.isBetMoney*odds;
						SoundManager.playerSound(SoundManager.MoveToSeat);
						_chipsCtrl.potToPlayer(tempP.seatId, tempP.seatId, xxFunc3);
						function xxFunc3():void {
							//刷新闲家的金币
							tempP.tableMoney += tempP.isBetMoney;
							refreshUserTableMoney(tempP.seatId, tempP.tableMoney);
							//刷新庄家的金币
							banker.tableMoney -= tempP.isBetMoney*odds;
							refreshUserTableMoney(banker.seatId, banker.tableMoney);
							//庄家要把陪的钱移动到奖池
							_chipsCtrl.playerBet(banker.seatId, bet, xxFunc4);
						}
						function xxFunc4():void {
							SoundManager.playerSound(SoundManager.MoveToSeat);
							//从奖池将钱移动到玩家
							_chipsCtrl.potToPlayer(banker.seatId, tempP.seatId, xxFunc5);
						}
						function xxFunc5():void {
							//刷新闲家的金币
							tempP.tableMoney += bet;
							refreshUserTableMoney(tempP.seatId, tempP.tableMoney);
							startFunc();
						}
					}
				}else {
					trace("结算完毕");
					resetNewGame();
					_cb(OPTION_TYPE_SETTLEMENT_COMPLETE);
				}
			}
		}
		/**
		 *重设新的一局 
		 * 
		 */
		private function resetNewGame():void {
			//初始化数据
			_tableData.resetANewGame();
			//初始化界面
//			_chipsCtrl.disposePlayerBet();
			var i:int;
			var len:int = 8;
			for(i=1; i<len; i++) {
				showHideCard(i, 0);
				showCardByLen(i, 0);
				showCowUi(false, i);
			}
		}
		
		/**
		 * 重设单个坐位
		**/
		private function resetSeat(seatid:int):void	{
			showHideCard(seatid, 0);
			showCardByLen(seatid, 0);
			showCowUi(false, seatid);
		} 
	}
}